﻿using Infragistics.Win.UltraWinGrid;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using Titan.FIS.Enum;
using Titan.FIS.UI;
using Titan.WinFormSys;

namespace Titan.FIS.SampleRecd
{
    public partial class frmSelfSample : FrmBase
    {
        private readonly IList<UltraGridRow> _listSelectRow = new List<UltraGridRow>();//选中的设备，用于打印
        /// <summary>
        /// 构造函数
        /// </summary>
        public frmSelfSample()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 窗体初始化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmCollect_Load(object sender, EventArgs e)
        {
            this.ActiveControl = txtRfid;

            Common.UserInfo = UserInfo;
            Common.MsgBox = ricMsg;


            DoBindCoalType();
            DoBindTeam();
            DoQuery("Load");
        }

        /// <summary>
        /// 窗体关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmCollect_FormClosed(object sender, FormClosedEventArgs e)
        {

        }
        /// <summary>
        /// 绑定物料
        /// </summary>
        private void DoBindTeam()
        {
            DataTable tabSource = null;
            var errMsg = DataOperate.DataQuery.DoQueryTeamInfo(ref tabSource);
            if (tabSource != null && tabSource.Rows.Count > 0)
            {
                tabSource.Rows.Add(tabSource.NewRow());
                txtTeam.DataSource = tabSource;
                txtTeam.ValueMember = "F_ID";
                txtTeam.DisplayMember = "F_NAME";
                txtTeam.SelectedIndex = -1;
            }
            else
                txtTeam.SelectedIndex = -1;
        }
        private void DoBindCoalType()
        {
            DataTable tabSource = null;
            var errMsg = DataOperate.DataQuery.DoQueryCoalTypeInfo(ref tabSource);
            if (tabSource != null && tabSource.Rows.Count > 0)
            {
                tabSource.Rows.Add(tabSource.NewRow());
                txtCoalType.DataSource = tabSource;
                txtCoalType.ValueMember = "F_ID";
                txtCoalType.DisplayMember = "F_NAME";
                txtCoalType.SelectedIndex = -1;
            }
            else
                txtCoalType.SelectedIndex = -1;
        }
        private void txtCoalType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (txtCoalType.SelectedIndex != -1)
                DoBindCoal(txtCoalType.SelectedValue.ToString());
        }
        /// <summary>
        /// 绑定物料
        /// </summary>
        private void DoBindCoal(string type)
        {
            DataTable tabSource = null;
            var errMsg = DataOperate.DataQuery.DoQueryCoalInfo(type, ref tabSource);
            if (tabSource != null && tabSource.Rows.Count > 0)
            {
                tabSource.Rows.Add(tabSource.NewRow());
                txtCoal.DataSource = tabSource;
                txtCoal.ValueMember = "F_ID";
                txtCoal.DisplayMember = "F_NAME";
                txtCoal.SelectedIndex = -1;
            }
            else
                txtCoal.SelectedIndex = -1;
        }
        /// <summary>
        /// 根据物料绑定供应商
        /// </summary>
        private void DoBindSupplier()
        {
            if (txtCoal.SelectedIndex >= 0)
            {
                DataTable tabSource = null;
                var errMsg = DataOperate.DataQuery.DoQuerySupplierInfo(txtCoal.SelectedValue.ToString(), ref tabSource);
                if (tabSource != null && tabSource.Rows.Count > 0)
                {
                    //tabSource.Rows.Add(tabSource.NewRow());
                    txtSupplier.DataSource = tabSource;
                    txtSupplier.ValueMember = "F_ID";
                    txtSupplier.DisplayMember = "F_NAME";
                    txtSupplier.SelectedIndex = 0;
                }
                else
                    txtSupplier.DataSource = null;
            }
            else
                txtSupplier.DataSource = null;
        }

        public string GetStypeCode(string strType)
        {
            switch (strType)
            {
                case "自检煤":
                    strType = "0";
                    break;
                case "槽煤":
                    strType = "1";
                    break;
                case "配合煤"://一班一个
                    strType = "2";
                    break;
                case "小焦炉煤":
                    strType = "3";
                    break;
                case "外样煤":
                    strType = "4";
                    break;
                case "火车煤":
                    strType = "5";
                    break;
                case "煤场煤":
                    strType = "6";
                    break;
                case "小焦炉焦炭":
                    strType = "7";
                    break;
                case "外样焦炭":
                    strType = "8";
                    break;
                case "自检焦炭"://一班一个
                    strType = "9";
                    break;
                case "出厂焦炭"://一天一个
                    strType = "10";
                    break;
            }
            return strType;
        }

        private void DoQuery(string QueryType)
        {
            string strDateTime = string.Empty;
            if (dtDate.Checked)
                strDateTime = dtDate.Value.ToString("yyyy-MM-dd");
            string strType = string.Empty, strCoal = string.Empty, strSupplier = string.Empty, strTeam = string.Empty, strRfid = string.Empty;
            if (!string.IsNullOrEmpty(txtType.Text) && txtType.Text != " ")
                strType = GetStypeCode(txtType.Text);
            if (!string.IsNullOrEmpty(txtCoal.Text))
                strCoal = txtCoal.SelectedValue.ToString();
            if (!string.IsNullOrEmpty(txtSupplier.Text))
                strSupplier = txtSupplier.SelectedValue.ToString();
            if (!string.IsNullOrEmpty(txtTeam.Text))
                strTeam = txtTeam.SelectedValue.ToString();
            strRfid = txtRfid.Text.Trim();
            DataTable tabSource = null;
            var errMsg = DataOperate.DataQuery.DoQuerySelfSampleInfo(strDateTime, strType, strCoal, strSupplier, strTeam, strRfid, ref tabSource);
            if (string.IsNullOrEmpty(errMsg))
            {
                for (int i = 0; i < tabSource.Rows.Count; i++)
                {
                    if (tabSource.Rows[i]["TYPE"].ToString() == "外样煤")
                        tabSource.Rows[i]["TEAM_NAME"] = "";
                }
                Titan.FIS.Data.MergeDataTable.MergeCaption(tabMain, ref tabSource);
                GridMain.DataSource = tabSource;
                if (QueryType == "Normal")
                    FormHelper.ApplicationShow(ricMsg, DisplayType.Run, "查询已完成！请在表格中查看相关数据");
            }
            else
            {
                GridMain.DataSource = tabMain;
                if (QueryType == "Normal")
                    FormHelper.ApplicationShow(ricMsg, DisplayType.Error, string.Format("查询失败，原因:{0}", errMsg));
            }
        }
        /// <summary>
        /// RFID号输入框点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtRfid_Click(object sender, EventArgs e)
        {
            txtRfid.Text = "";
        }


        private void btnRead_Click(object sender, EventArgs e)
        {
            txtRfid.Text = "";
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            if (GridMain.ActiveRow == null)
            {
                FormHelper.ApplicationShow(ricMsg, DisplayType.Error, "请先点击想要复制的行!");
                return;
            }
            var collectCode = GridMain.ActiveRow.Cells["BATCH_CODE"].Value.ToString();
            if (!string.IsNullOrEmpty(collectCode))
            {
                Clipboard.SetText(collectCode);
                FormHelper.ApplicationShow(ricMsg, DisplayType.Run, string.Format("复制制样编码【{0}】成功！", collectCode));
            }
            else
            {
                FormHelper.ApplicationShow(ricMsg, DisplayType.Error, string.Format("制样编码为空，请检查！"));
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            DoQuery("Normal");
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string strDate = string.Empty, strType = string.Empty, strCoal = string.Empty, strSupplier = string.Empty, strTeam = string.Empty, strRfid = string.Empty;
            if (!dtDate.Checked)
            {
                FormHelper.ApplicationShow(ricMsg, DisplayType.Error, "请先选择日期!");
                return;
            }
            strDate = dtDate.Value.ToString("yyyy-MM-dd");
            var strQuerstion = $"日期【{strDate}】";
            if (string.IsNullOrEmpty(txtType.Text.Trim()))
            {
                FormHelper.ApplicationShow(ricMsg, DisplayType.Error, "请先选择类型!");
                return;
            }
            strType = GetStypeCode(txtType.Text);
            strQuerstion = strQuerstion + $"类型【{txtType.Text.Trim()}】";

            if (!string.IsNullOrEmpty(txtCoal.Text.Trim()))
            {
                strCoal = txtCoal.SelectedValue.ToString();
                strQuerstion = strQuerstion + $"煤种【{txtCoal.Text.Trim()}】";
            }
            if (!string.IsNullOrEmpty(txtSupplier.Text))
            {
                strSupplier = txtSupplier.SelectedValue.ToString();
                strQuerstion = strQuerstion + $"供应商【{txtSupplier.Text.Trim()}】";
            }
            if (!string.IsNullOrEmpty(txtTeam.Text))
            {
                strTeam = txtTeam.SelectedValue.ToString();
                strQuerstion = strQuerstion + $"班组【{txtTeam.Text.Trim()}】";
            }
            if (!string.IsNullOrEmpty(txtTrough.Text))
            {
                strTeam = txtTrough.Text.ToString();//避免修改存储过程参数，共用班组字段传递数据
                if (txtType.Text.Trim() == "火车煤" || txtType.Text.Trim() == "煤场煤" || txtType.Text.Trim() == "小焦炉煤" || txtType.Text.Trim() == "小焦炉焦炭")
                    strQuerstion = strQuerstion + $"序号【{txtTrough.Text.Trim()}】";
                else
                    strQuerstion = strQuerstion + $"槽位【{txtTrough.Text.Trim()}】";
            }
            if (!string.IsNullOrEmpty(txtRfid.Text))
            {
                strRfid = txtRfid.Text.Trim();
                strQuerstion = strQuerstion + $"Rfid【{strRfid}】";
            }

            if ((txtType.Text.Trim() != "配合煤" && txtType.Text.Trim() != "自检焦炭" && txtType.Text.Trim() != "出厂焦炭") && txtType.Text != "外样煤" && txtType.Text != "外样焦炭" && string.IsNullOrEmpty(strCoal))
            {
                FormHelper.ApplicationShow(ricMsg, DisplayType.Error, "请先选择煤种!");
                return;
            }
            if ((txtType.Text.Trim() == "配合煤" || txtType.Text.Trim() == "自检焦炭") && string.IsNullOrEmpty(strTeam))
            {
                FormHelper.ApplicationShow(ricMsg, DisplayType.Error, "请先选择班组!");
                return;
            }
            if (txtType.Text.Trim() == "槽煤" && string.IsNullOrEmpty(strTeam))
            {
                FormHelper.ApplicationShow(ricMsg, DisplayType.Error, "请先选择槽位!");
                return;
            }
            if ((txtType.Text.Trim() == "火车煤" || txtType.Text.Trim() == "煤场煤" || txtType.Text.Trim() == "小焦炉煤" || txtType.Text.Trim() == "小焦炉焦炭")  && string.IsNullOrEmpty(strTeam))
            {
                FormHelper.ApplicationShow(ricMsg, DisplayType.Error, "请先选择序号!");
                return;
            }
            if (txtType.Text == "外样煤" || txtType.Text == "外样焦炭")
            {
                strTeam = DateTime.Now.ToString("HHmmss");
            }
            strQuerstion = string.Format("请确认是否添加{0}的自检样记录？", strQuerstion);
            if (MessageBox.Show(strQuerstion, "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                var strErrInfo = string.Empty;
                if (!string.IsNullOrEmpty(strRfid))
                {
                    var bCheckResult = DataOperate.DataQuery.DoCheckPail(strRfid, ref strErrInfo);
                    if (!bCheckResult)
                    {
                        FormHelper.ApplicationShow(ricMsg, DisplayType.Error, string.Format("检查样桶信息是否未收样失败,原因:{0}", strErrInfo));
                        return;
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(strErrInfo))
                        {
                            if (MessageBox.Show(string.Format("该样桶【{0}】存在未收样汽采记录,请确认是否使用该桶继续添加？", strRfid), "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                            {
                                return;
                            }
                        }
                    }
                }
                var strBatchCode = string.Empty;
                var bSaveResult = DataOperate.DataExecProc.DoAddSelfSample(strDate, strType, strCoal, strSupplier, strTeam, strRfid, ref strBatchCode, ref strErrInfo);
                if (!bSaveResult)
                {
                    FormHelper.ApplicationShow(ricMsg, DisplayType.Error, string.Format("添加自检样记录失败,原因:{0}", strErrInfo));
                }
                else
                {
                    FormHelper.ApplicationShow(ricMsg, DisplayType.Run, string.Format("添加自检样记录成功!批次编码【{0}】", strBatchCode));
                    if (txtType.Text == "小焦炉煤")
                    {
                        strType = GetStypeCode("小焦炉焦炭");
                        var bSaveResultPlus = DataOperate.DataExecProc.DoAddSelfSample(strDate, strType, strCoal, strSupplier, strTeam, strRfid, ref strBatchCode, ref strErrInfo);
                        if (!bSaveResultPlus)
                        {
                            FormHelper.ApplicationShow(ricMsg, DisplayType.Error, string.Format("添加自检样记录失败,原因:{0}", strErrInfo));
                        }
                        else
                        {
                            FormHelper.ApplicationShow(ricMsg, DisplayType.Run, string.Format("添加自检样记录成功!批次编码【{0}】", strBatchCode));
                        }
                    }
                    txtType.SelectedIndex = -1;
                    txtCoalType.SelectedIndex = -1;
                    txtCoal.SelectedIndex = -1;
                    txtSupplier.SelectedIndex = -1;
                    txtTeam.SelectedIndex = -1;
                    txtTrough.SelectedIndex = -1;
                    txtRfid.Text = "";
                    dtDate.Value = DateTime.Now;
                    dtDate.Checked = true;
                    DoQuery("ADD");
                }
            }
        }

        private void txtType_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtTrough.SelectedIndex = -1;
            txtTrough.Enabled = false;
            if (txtType.Text == "配合煤" || txtType.Text == "自检焦炭")
            {
                txtCoal.SelectedIndex = -1;
                txtCoal.Enabled = false;
                txtCoalType.SelectedIndex = -1;
                txtCoalType.Enabled = false;
                txtTeam.Enabled = true;
            }
            else if (txtType.Text == "出厂焦炭")
            {
                txtCoal.SelectedIndex = -1;
                txtCoal.Enabled = false;
                txtCoalType.SelectedIndex = -1;
                txtCoalType.Enabled = false;
                txtTeam.SelectedIndex = -1;
                txtTeam.Enabled = false;
            }
            else if (txtType.Text == "外样煤" || txtType.Text == "外样焦炭")
            {
                txtCoal.SelectedIndex = -1;
                txtCoal.Enabled = false;
                txtCoalType.SelectedIndex = -1;
                txtCoalType.Enabled = false;
                txtTeam.SelectedIndex = -1;
                txtTeam.Enabled = false;

                txtTrough.SelectedIndex = -1;
                txtTrough.Enabled = false;


            }
            else
            {
                txtTeam.SelectedIndex = -1;
                txtTeam.Enabled = false;
                txtCoal.Enabled = true;
                txtCoalType.Enabled = true;
                if (txtType.Text == "槽煤" || txtType.Text == "火车煤" || txtType.Text == "煤场煤" || txtType.Text == "小焦炉煤" || txtType.Text == "小焦炉焦炭")
                {
                    txtTrough.Enabled = true;
                    ultraLabel8.Text = "槽位";
                }
            }
            if (txtType.Text == "火车煤" || txtType.Text == "煤场煤" || txtType.Text == "小焦炉煤" || txtType.Text == "小焦炉焦炭")
                ultraLabel8.Text = "序号";
            else
                ultraLabel8.Text = "槽位";

        }

        private void txtCoal_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (txtCoal.SelectedIndex >= 0)
            {
                DoBindSupplier();
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (GridMain.ActiveRow == null)
            {
                FormHelper.ApplicationShow(ricMsg, DisplayType.Error, "请先点击想要打印的行!");
                return;
            }
            var collectCode = GridMain.ActiveRow.Cells["BATCH_CODE"].Value.ToString();
            if (!string.IsNullOrEmpty(collectCode))
            {
                DoPrint(collectCode);
            }
            else
            {
                FormHelper.ApplicationShow(ricMsg, DisplayType.Error, string.Format("制样编码为空，请检查！"));
            }


        }

        /// <summary>
        /// 打印批次编码
        /// </summary>
        private void DoPrint(string code)
        {
            var collectCode = code;
            if (MessageBox.Show(string.Format("是否立即打印制样编码【{0}】？", collectCode), "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                return;
            }

            var gen = new TagGen();
            var img = gen.GetImgASample(collectCode);
            var obj = new TagPrint(img, 1);
            if (!obj.Print())
            {
                FormHelper.ApplicationShow(ricMsg, DisplayType.Error, string.Format("打印制样编码{0}失败!", collectCode));
            }
            FormHelper.ApplicationShow(ricMsg, DisplayType.Run, string.Format("打印制样编码{0}成功!", collectCode));
        }


    }

}
